<?php
declare(strict_types=1);


namespace App\Fastapp\Mapper;

use App\Fastapp\Model\FastappRobotGroup;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 群组列表Mapper类
 */
class FastappRobotGroupMapper extends AbstractMapper
{
    /**
     * @var FastappRobotGroup
     */
    public $model;

    public function assignModel()
    {
        $this->model = FastappRobotGroup::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 来源1tg
        if (isset($params['source']) && filled($params['source'])) {
            $query->where('source', '=', $params['source']);
        }

        // 来源用户
        if (isset($params['source_user']) && filled($params['source_user'])) {
            $query->where('source_user', '=', $params['source_user']);
        }

        // 1普通群2超级群3频道
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 
        if (isset($params['shop_id']) && filled($params['shop_id'])) {
            $query->where('shop_id', '=', $params['shop_id']);
        }

        // 群id
        if (isset($params['group_id']) && filled($params['group_id'])) {
            $query->where('group_id', '=', $params['group_id']);
        }

        // 名称
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        return $query;
    }
}